#include "saolei.h"
#include "ui_saolei.h"

static int paint_flag = 0;
QPainter * paintmywindow;
saolei * app;
int ROW =9;
int COL =9;
int BOOMNUM = 10;
block * mine = new block(ROW,COL,BOOMNUM);
int click_x,click_y,end = 0,timeend=1;
int LENGTH=mine->getcol()*21;
int HEIGHT=mine->getrow()*21 + TITILE_HEIGH;
static bool showimage = 1;

saolei::saolei(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::saolei)
{
    ui->setupUi(this);
    this->setMinimumSize(LENGTH,HEIGHT);
    this->setMaximumSize(LENGTH,HEIGHT);
    runtime = new QTimer(this);
    connect(runtime,SIGNAL(timeout()),this,SLOT(on_secondadd()));
    connect(runtime,SIGNAL(timeout()),this,SLOT(update()));
}

saolei::~saolei()
{
    delete ui;
}

void saolei::mousePressEvent(QMouseEvent *event)
{
     click_x = event->x();
     click_y = event->y();
     if(event->buttons()==(Qt::LeftButton)&&(click_x>(int)LENGTH*100/200-11)&&
             (click_x<(int)LENGTH*100/200+11 +EXPRESSION_LENGHT)&&(click_y>7+TTILE_BEGIN)&&
             (click_y<7+TTILE_BEGIN+EXPRESSION_HEIGH))
    {
     qDebug()<<"点击了笑脸";
     restart(mine->getrow(),mine->getcol(),mine->getboomNum());
     paint_flag = 0;
     end = 0;
     timeend = 1;
     runtime->stop();
     update();
     return;
    }
    else if( click_y<TITILE_HEIGH ) return;

    if(end==1)
    return ;
    click_x = click_x/BLOCK_LENGTH;
    click_y = (click_y-TITILE_HEIGH)/BLOCK_HEIGH;
    if(event->buttons()==(Qt::LeftButton))
     {
         if(timeend==1)
         {
             runtime->start(1000);
         }
         if(mine->p[click_y][click_x]==99)
         {
             paint_flag = 4;
             end = 1;
             timeend=1;
             runtime->stop();
             update();

         }
         else if((mine->p[click_y][click_x]<10))
         {
             showblock(click_y,click_x);
             paint_flag = 2;
             if(end = iswin()==1)
             {
                 paint_flag = 5;
                 timeend=1;
                 runtime->stop();
             }
             update();
             if(end)
         {
             if(showimage)
              {
                  QMessageBox message(QMessageBox::NoIcon, "you are win", "very good, I really very appreciate you ");
                  message.setIconPixmap(QPixmap("win.png"));
                  showimage = !showimage;
                  message.exec();
              }
             else
             {
                  QMessageBox message(QMessageBox::NoIcon, "you are win", " I will give you a red flower ");
                  message.setIconPixmap(QPixmap("good.png"));
                  showimage = !showimage;
                  message.exec();
             }

         }
         }
          else return;

     }
    else if(event->buttons()==(Qt::RightButton))
     {
         if((mine->p[click_y][click_x]<10)||(mine->p[click_y][click_x]==99))
         {
             mine->p[click_y][click_x] = mine->p[click_y][click_x] +50;
             mine->minusredFlagNum();
         }
         else if((mine->p[click_y][click_x]>=50&&mine->p[click_y][click_x]<60)||(mine->p[click_y][click_x]>110))
         {
             mine->p[click_y][click_x] = mine->p[click_y][click_x] -50;
             mine->addredFlagNum();
         }
         paint_flag = 3;
         if(end = iswin()==1)
         {
             paint_flag = 5;
             timeend=1;
             runtime->stop();
         }
         update();
         if(end)
         {
             if(showimage)
              {
                  QMessageBox message(QMessageBox::NoIcon, "you are win", "very good, I really very appreciate you ");
                  message.setIconPixmap(QPixmap("win.png"));
                  showimage = !showimage;
                  message.exec();
              }
             else
             {
                  QMessageBox message(QMessageBox::NoIcon, "you are win", " I will give you a red flower ");
                  message.setIconPixmap(QPixmap("good.png"));
                  showimage = !showimage;
                  message.exec();
             }
         }
    }
}

void saolei::paintEvent(QPaintEvent *event)
{
     QPainter painter(this);
     paintmywindow = &painter;
     QPixmap mine_title(":/new/prefix1/扫雷工程配套图片/mine_title.bmp");
     QPixmap block_close(":/new/prefix1/扫雷工程配套图片/block_close.bmp");
     QPixmap block_open(":/new/prefix1/扫雷工程配套图片/block_open.bmp");
     QPixmap red_flag(":/new/prefix1/扫雷工程配套图片/redflag.bmp");
     QPixmap keil_boom(":/new/prefix1/扫雷工程配套图片/keilboom.bmp");
     QPixmap click_boom(":/new/prefix1/扫雷工程配套图片/clickboom.bmp");
     QPixmap boom(":/new/prefix1/扫雷工程配套图片/boom.bmp");
     QPixmap qq_smile(":/new/prefix1/扫雷工程配套图片/smile.bmp");
     QPixmap qq_lose(":/new/prefix1/扫雷工程配套图片/lose.bmp");
     QPixmap qq_pround(":/new/prefix1/扫雷工程配套图片/proud.bmp");
     QPixmap ele_0(":/new/prefix1/扫雷工程配套图片/ele0.bmp");
     QPixmap ele_1(":/new/prefix1/扫雷工程配套图片/ele1.bmp");
     QPixmap ele_2(":/new/prefix1/扫雷工程配套图片/ele2.bmp");
     QPixmap ele_3(":/new/prefix1/扫雷工程配套图片/ele3.bmp");
     QPixmap ele_4(":/new/prefix1/扫雷工程配套图片/ele4.bmp");
     QPixmap ele_5(":/new/prefix1/扫雷工程配套图片/ele5.bmp");
     QPixmap ele_6(":/new/prefix1/扫雷工程配套图片/ele6.bmp");
     QPixmap ele_7(":/new/prefix1/扫雷工程配套图片/ele7.bmp");
     QPixmap ele_8(":/new/prefix1/扫雷工程配套图片/ele8.bmp");
     QPixmap ele_9(":/new/prefix1/扫雷工程配套图片/ele9.bmp");
     QPixmap blk_1(":/new/prefix1/扫雷工程配套图片/blk1.bmp");
     QPixmap blk_2(":/new/prefix1/扫雷工程配套图片/blk2.bmp");
     QPixmap blk_3(":/new/prefix1/扫雷工程配套图片/blk3.bmp");
     QPixmap blk_4(":/new/prefix1/扫雷工程配套图片/blk4.bmp");
     QPixmap blk_5(":/new/prefix1/扫雷工程配套图片/blk5.bmp");
     QPixmap blk_6(":/new/prefix1/扫雷工程配套图片/blk6.bmp");
     QPixmap blk_7(":/new/prefix1/扫雷工程配套图片/blk7.bmp");
     QPixmap blk_8(":/new/prefix1/扫雷工程配套图片/blk8.bmp");


    //如果是第一次打开，则进行初始化
    if(paint_flag==0)
    {
        painttitle(paintmywindow);
        //绘制雷区
        for(int i = 0;i<mine->getcol()*BLOCK_LENGTH;i=i+BLOCK_LENGTH)
            for(int j =TITILE_HEIGH;j<TITILE_HEIGH+mine->getrow()*BLOCK_HEIGH;j=j+BLOCK_HEIGH)
              painter.drawPixmap(i,j,block_close,0,0,BLOCK_LENGTH,BLOCK_LENGTH);
    }
    //左键点到炸弹绘制
    else if (paint_flag == 4)
    {
        painttitle(paintmywindow);
        //绘制雷区
        for(int i = 0;i<mine->getrow();i++)
        {
            for(int j = 0;j<mine->getcol();j++ )
               {
                if((mine->p[i][j]<10))
                {
                    painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,block_close,0,0,BLOCK_LENGTH,BLOCK_HEIGH);
                }
                else if((mine->p[i][j]==99))
                {
                    painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,boom,0,0,BLOCK_LENGTH,BLOCK_HEIGH);
                }
                else if (mine->p[i][j]>110)
                {
                    painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,red_flag,0,0,BLOCK_LENGTH,BLOCK_HEIGH);
                }
                else if ((mine->p[i][j]<60)&&(mine->p[i][j]>=50))
                {
                    painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,keil_boom,0,0,BLOCK_LENGTH,BLOCK_HEIGH);
                }
                else if ((mine->p[i][j])>99&&(mine->p[i][j]<110))
                {
                   int blk_num = mine->p[i][j] - 100;
                   switch (blk_num)
                   {
                    case 0: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,block_open,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 1: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_1,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 2: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_2,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 3: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_3,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 4: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_4,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 5: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_5,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 6: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_6,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 7: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_7,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                    case 8: painter.drawPixmap(j*BLOCK_LENGTH,i*BLOCK_HEIGH+TITILE_HEIGH,blk_8,0,0,BLOCK_LENGTH,BLOCK_HEIGH);break;
                   }
                }
               }
            }
        //绘制点击的炸弹
        painter.drawPixmap(click_x*BLOCK_LENGTH,click_y*BLOCK_HEIGH+TITILE_HEIGH,click_boom,0,0,BLOCK_LENGTH,BLOCK_HEIGH);

     }
    //左键点到空白绘制
    else if (paint_flag == 2 )
    {
       //绘制标题
       painttitle(paintmywindow);
       //绘制雷区
       paintboom(paintmywindow);
    }
    //右键点击绘制
    else if(paint_flag ==3)
    {

        painttitle(paintmywindow);
        paintboom(paintmywindow);
    }
    //游戏成功结束
    else if(paint_flag == 5)
    {
        painttitle(paintmywindow);
        paintboom(paintmywindow);
    }
}

void showblock(int x,int y)
{
    for(int blockRow = x - 1; blockRow <=x + 1;blockRow++)
        for(int blockCol = y - 1;blockCol <=y + 1;blockCol++)
        {
            if(blockRow<0||blockCol<0||blockRow>=mine->getrow()||blockCol>=mine->getcol())
               continue;
            if(mine->p[blockRow][blockCol]<10)
            {
                mine->p[blockRow][blockCol] =  mine->p[blockRow][blockCol] + 100;
                if(mine->p[blockRow][blockCol]==100)
                showblock(blockRow,blockCol);
            }
        }
}
